package com.aw.guiguzi.module.manager._user

import android.view.View
import android.widget.CompoundButton
import android.widget.Switch
import android.widget.TextView
import com.aw.baselib.base.AwBaseEvent
import com.aw.baselib.base.AwMvpBindingActivity
import com.aw.baselib.constants.Extras
import com.aw.baselib.util.AwDataUtil
import com.aw.guiguzi.R
import com.aw.guiguzi.bean.UserInfoBean
import com.aw.guiguzi.constants.MyConstant
import com.aw.guiguzi.databinding.ActivityManagerUserDetailBinding
import com.aw.guiguzi.event.EventRefreshUserDetailType
import com.aw.guiguzi.event.EventRefreshUserListType
import com.aw.guiguzi.module.manager._user.presenters.UserDetailPresent
import com.aw.guiguzi.module.manager._user.views.UserDetailView

class UserDetailActivity :
    AwMvpBindingActivity<UserDetailPresent?, ActivityManagerUserDetailBinding?>(),
    UserDetailView.View {
    private var mBean: UserInfoBean? = null
    override fun refreshByBus(type: AwBaseEvent) {
        super.refreshByBus(type)
        if (type is EventRefreshUserDetailType) {
            mPresenter!!.getUserDetail(mBean!!.id)
        }
    }

    override fun createPresenter(): UserDetailPresent {
        return UserDetailPresent(this)
    }

    override fun getViewBinding(): ActivityManagerUserDetailBinding {
        return ActivityManagerUserDetailBinding.inflate(layoutInflater)
    }

    override fun initView() {
        super.initView()
        setToolbar("用户详情", null)
    }

    override fun initData() {
        super.initData()
        mBean = intent.getSerializableExtra(Extras.params) as UserInfoBean?
        if (mBean == null) {
            showDialogToFinish("数据异常")
            return
        }
        setViewData()
        mPresenter!!.getUserDetail(mBean!!.id)
    }

    override fun initListener() {
        super.initListener()
        binding?.tvResetPwd!!.setOnClickListener { v: View? ->
            toClass(
                UserResetPwdActivity::class.java, false, Extras.params, mBean
            )
        }
        binding?.tvDelAccount!!.setOnClickListener { v: View? ->
            showDialogWithCancelDismiss("确认删除该用户么？") { v1: View? ->
                mPresenter!!.delUser(
                    mBean!!.id
                )
            }
        }
        binding?.switchUserStatus!!.setOnCheckedChangeListener { buttonView: CompoundButton?, isChecked: Boolean ->
            mPresenter!!.updateStatus(
                mBean!!.id, isChecked
            )
        }
        findViewById<View>(R.id.rl_nickname).setOnClickListener { v: View? ->
            toClass(
                UserEditActivity::class.java,
                false,
                Extras.params,
                mBean,
                Extras.params2,
                MyConstant.USER_EDIT_TAG_NICKNAME
            )
        }
    }

    private fun setViewData() {
        if (mBean == null) {
            return
        }
        setText(binding?.tvUserId, mBean!!.id)
        setText(binding?.tvUsername, mBean!!.username)
        setText(binding?.tvNickname, mBean!!.nickname)
        setText(binding?.tvUserPhone, mBean!!.mobile)
        setText(
            binding?.tvUserDevice,
            if (AwDataUtil.isEmpty(mBean!!.email)) "" else mBean!!.email!!.split("@".toRegex())
                .dropLastWhile { it.isEmpty() }
                .toTypedArray()[0])
        binding?.switchUserStatus!!.isChecked = mBean!!.isEnable
    }

    override fun getUserDetailSuccess(bean: UserInfoBean?) {
        mBean = bean
        setViewData()
    }

    override fun getUserDetailFail(code: Int, msg: String?) {}
    override fun updateStatusSuccess() {
        mPresenter!!.getUserDetail(mBean!!.id)
    }

    override fun updateStatusFail(code: Int, msg: String?) {
        setViewData()
    }

    override fun delUserSuccess() {
        showMsg("删除用户成功")
        postEvent(EventRefreshUserListType(null))
        finish()
    }
}